var common = require("../../utils/common.js");
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    data: [],
    banner: '',
    tip: [],
    longitude: 118.6648443788737,
    latitude: 31.90991590886252,
    getUserInfoFail: false,
    type: '',//分享进来的各种数据
    id: '',//如果是顾问详情页分享进来的，则有
    maskTime: '',
    show: false,
    music: false,
    canIUse: '',


  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (options) {
    // console.log(options);
    //分享测试
    //从roomDetail进来的页面将id赋给了全局参数lpid，分享的id不会受影响
    var o = options;
    this.setData({
      oInf: options
    })
    app.globalData.shareBid = options.bid;//options.bid;//用shareBid接收bid,避免数据被占用,避免分享链接重复进入失败
    //if (o.lpid) {
      // console.log("分享有lpid"+o.lpid)
     // this.setData({
      //  lpid: o.lpid
     // })
    //  app.globalData.lpid = o.lpid
  //  }
    if (o.xcxid) {
      // console.log("分享有lpid"+o.lpid)
      this.setData({
        xcxid: o.xcxid
      })
    }

    if (o.forumid) {
      this.setData({
        forumid: o.forumid,
        xcxid: o.xcxid
      })
    }
    // console.log(this.data.oInf);
    if (o.type) {
      this.setData({
        type: o.type
      })
    } if (o.id) {
      this.setData({
        id: o.id
      })
    }
    if (o.isIndex) {
      this.setData({
        isIndex: o.isIndex
      })
    }
    if(this.data.isIndex=="1"){
      //分享进来的(有问题)
      app.globalData.isShareBack = false;
    }else{
      app.globalData.isShareBack = true;
    }
  },

  onReady: function () {
    // console.log("onReady")
    this.videoContext = wx.createVideoContext('myVideo');
    //this.videoContext.requestFullScreen(); //设置全屏
    // this.videoContext.hideStatusBar();//隐藏状态栏
  },
  bindended: function () {
    //console.log("播完了")
    this.videoContext = wx.createVideoContext('myVideo');
    this.videoContext.exitFullScreen(); //设置全屏
    this.setData({
      videoShow: false
    })
    this.load();
  },
  load: function () {
    var that = this;
    that.toPage()
  },

  changeMusic: function () {
    this.setData({
      show: !this.data.show,
      music: !this.data.music
    })
  },

  loads: function () {
    var that = this;
    var lpid;//从roomDetail进来
    if (app.globalData.lpid){
      lpid = app.globalData.lpid
    } else if (that.data.oInf.bid){
      lpid = that.data.oInf.bid;
    }else{
      lpid =that.data.bid;
    }

    if(app.globalData.shareBid){
      app.globalData.bid=app.globalData.shareBid
    }

    //1.从roomDetail进来必有lpid,分享不一定有lpid
    common.ajaxPost(
      'portal/wlpBuilding/queryBuilding.action', {
        id: lpid
      },
      function (res) {
        if (res && res.length > 0) {
          //  console.log(res);
          var data = res[0];
          var bid = data.id;
          var vrUrl = data.vrUrl;
          var pname = data.name
          //bid获取
          //app.globalData.bid = bid;
          app.globalData.vrUrl = vrUrl;
          //根据maskType修改蒙层
          var maskType = data.maskType;
          if (that.data.isIndex) {
            that.load();
          } else {
            if (maskType == '0') {
              //无
              that.load();
            } else if (maskType == '1') {
              //图片
              var maskUrl = data.maskUrl;
              var maskTime = data.maskTime;
              that.setData({
                maskType: maskType,
                maskUrl: maskUrl,
                maskTime: maskTime,
                seconds: maskTime
              })
              that.currentTime();
            } else if (maskType == '2') {
              //视频
              var maskUrl = data.maskUrl;
              that.setData({
                maskType: maskType,
                maskUrl: maskUrl,
              })
            }
          }
          wx.setNavigationBarTitle({
            title: pname
          });
          wx.hideLoading()
        }
      }
    )
  },
  currentTime: function () {
    //倒计时
    var that = this;
    var beginTime = that.data.maskTime;
    var startTime = that.data.seconds;
    // console.log(beginTime);
    var times = setTimeout(function () {
      if (beginTime == '0') {
        that.load();
        //  that.setData({
        //    maskTime: startTime
        //  })
        return false;
      }
      else {
        beginTime--;
        that.setData({
          maskTime: beginTime
        })
        that.currentTime();
      }
    }, 1000);
  },
  onShow: function () {
    if (app.globalData.queryWlpXcxByParams) {

    } else {
      this.queryWlpXcxByParams();
    }
    // console.log("index页面的show");
    var that = this;
    wx.login({
      success: res => {
        if (res.code) {
          var code = res.code;
          //获取xcxOpenId;
          // 获取用户信息
          wx.getSetting({
            success: res => {
              if (res.authSetting['scope.userInfo']) {
                console.log("已授权登录1");
                wx.getUserInfo({
                  success: function (res) {
                    console.log(res.userInfo)
                    app.globalData.alluserInfo = res.userInfo;
                    app.globalData.isFind = true;
                    console.log("code=" + code);
                    that.getOpenId(code);
                  }
                })
              } else {
                //未授权登录
                console.log("未授权登录");
                app.globalData.isFind = false;
                that.getOpenId(code);
                that.setData({
                  canIUse: true
                })
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg);
        }
      }
    })



    // this.loads();
  },

  toPage: function () {
    var that = this;
    //分享页获取
    //app.globalData.xcxshareid = that.data.oInf.xcxid;
    app.globalData.loading = true;
    that.setData({
      loading: app.globalData.loading
    })
    if (app.globalData.isShareBack) {
      app.globalData.isShareBack = false;
      app.globalData.loading = false;
      wx.switchTab({
        url: '/pages/shouye/shouye'
      })
      return false;
    }
    if (that.data.type == 'adviserDetail') {
      wx.navigateTo({
        url: '/pages/adviserDetail/adviserDetail?id=' + that.data.oInf.id + '&usertype=' + that.data.oInf.usertype + '&hasNoReceive=' + that.data.oInf.hasNoReceive + '&position=' + that.data.oInf.position + '&weixin=' + that.data.oInf.weixin + '&weixinQrcode=' + that.data.oInf.weixinQrcode + '&phone=' + that.data.oInf.phone + '&headimage=' + that.data.oInf.headimage + '&name=' + that.data.oInf.name + "&xcxid=" + that.data.oInf.xcxid + "&bid=" + that.data.oInf.bid + "&lpid=" + app.globalData.oInf.lpid,
        success: function () {
          app.globalData.loading = false;
        }
      })
      return false;
    } else if (that.data.type == 'adviserList') {
      wx.navigateTo({
        url: '/pages/adviserList/adviserList?xcxid=' + that.data.oInf.xcxid,
        success: function () {
          //  console.log("adviserList分享");
          //  console.log(that.data.oInf.xcxid)
          app.globalData.loading = false;
        }
      })
      return false;
    } else if (that.data.type == 'detail') {
      app.globalData.loading = false;
      app.globalData.bid = that.data.oInf.bid;
      console.log(that.data.oInf.bid);
      wx.switchTab({
        url: '/pages/detail/detail'
      })
      return false;
    }
    else if (that.data.type == 'forum') {
      //xcxshareid用来接收xcxid
      app.globalData.loading = false;
      app.globalData.xcxshareid = that.data.oInf.xcxid
      wx.switchTab({
        url: '/pages/forum/forum'
      })
      return false;
    }
    else if (that.data.type == 'webView') {
      wx.navigateTo({
        url: '/pages/webView/webView?vrUrl=' + app.globalData.vrUrl,
        success: function () {
          app.globalData.loading = false;
        }
      })
      return false;
    }
    else if (that.data.type == 'forumDetail') {
      //  console.log("我进来了");
      // console.log(this.data.forumid);
      wx.navigateTo({
        url: '/pages/forumDetail/forumDetail?forumid=' + that.data.oInf.forumid + "&xcxid=" + that.data.oInf.xcxid,
        success: function () {
          app.globalData.loading = false;
        }
      })
      return false;
    }


    else if (that.data.type == 'order') {
      wx.navigateTo({
        url: '/pages/order/order',
        success: function () {
          app.globalData.loading = false;
        }
      })
      return false;
    } else if (that.data.type == 'photos') {
      app.globalData.loading = false;
      wx.switchTab({
        url: '/pages/photos/photos'
      })
      return false;
    } else if (that.data.type == 'myAdviser') {
      var bid;
      if (app.globalData.shareBid){
        bid = app.globalData.shareBid;
      }else{
        bid = that.data.oInf.bid
      }
      wx.navigateTo({
        url: '/pages/myAdviser/myAdviser?id=' + that.data.id + '&bid=' + bid,
        success: function () {
          app.globalData.loading = false;
        }
      })
      return false;
    } else {
      app.globalData.loading = false;
      wx.switchTab({
        url: '/pages/shouye/shouye'
      })
      return false;
    }
  },

  getOpenId: function (code) {
    var that = this;
    var xcxid= app.globalData.queryWlpXcxByParams[0].id;
    common.ajaxPost(
      'portal/wlpUsers/getXcxOpenId.action', {
        // bid: app.globalData.bid,
        code: code,
        xcxId: xcxid
      },
      function (res) {
        if (res && res["result"]) {
          console.log(res);
          app.globalData.xcxOpenId = res.xcxOpenId;
          // that.findUser();
          if (app.globalData.isFind) {
            //已授权
            that.findUser();
          } else {
            //未授权
          }
        }
      }
    )
  },


  openSetting: function (e) {
    console.log(e);
    var that = this;
    if (e.detail.errMsg == 'getUserInfo:fail auth deny') {
      return false;
    } else {
      console.log(e.detail.userInfo);
      app.globalData.userInfoweixin = e.detail.userInfo;
      //授权时查询用户
      that.setData({
        canIUse: false,
        loading: true
      });
      that.findUser();
    }
  },


  findUser: function () {
    var that = this;
    //查询用户是否存在
    var bid;
    if(app.globalData.bid){
      bid=app.globalData.bid;
    } else if (that.data.bid){
      bid = that.data.bid
    }else{
      bid = that.data.oInf.bid
    }
    console.log("bid="+bid);
    common.ajaxPostNo(
      'portal/wlpUsers/queryUserInfo.action', {
        xcxOpenId: app.globalData.xcxOpenId,
        bid: bid,
        userType: 2
      },
      function (res) {
        if (res.data.resultCode == "1000") {
          app.globalData.userInfo = res.data.userInfo;
          that.loads();
        } else {
          //用户不存在
          that.modifyUser();
        }
        return false;
      }
    )
  },

  modifyUser: function () {
    //保存用户信息
    var that = this;
    var bid;
    if (app.globalData.bid) {
      bid = app.globalData.bid;
    } else if (that.data.bid) {
      bid = that.data.bid
    } else {
      bid = that.data.oInf.bid
    }
    common.ajaxPost(
      'portal/wlpUsers/modifyUser.action', {
        userType: 2,
        headImage: app.globalData.userInfoweixin.avatarUrl,
        name: app.globalData.userInfoweixin.nickName,
        bid: bid,
        xcxOpenId: app.globalData.xcxOpenId,
      },
      function (res) {
        console.log(res);
        if (res && res["result"]) {
          if (res.resultMsg == '操作成功') {
            //添加成功，显示列表页
            that.setData({
              canIUse: false,
            });
            that.loads();
          } else {
            //添加成功，显示列表页
            that.setData({
              canIUse: false,
            });
          }
        }
      }
    )
  },

  queryWlpXcxByParams: function () {
    var _that = this;
    common.ajaxPost(
      'portal/wlpXcx/queryWlpXcxByParams.action', {
        appId: 'wx067ac066b8ee77fd'
      },
      function (res) {
        //console.log(res);
        if (res.data) {
          app.globalData.queryWlpXcxByParams = res.data;
        }
      })
  },
})
